package org.jeecg.modules.goods.whsupplier.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.goods.whsupplier.entity.WhSupplier;
import org.jeecg.modules.goods.whsupplier.mapper.WhSupplierMapper;
import org.jeecg.modules.goods.whsupplier.service.IWhSupplierService;
import org.jeecg.modules.tplus.dto.PartnerQueryRepDto;
import org.jeecg.modules.tplus.dto.PartnerQueryReqDto;
import org.jeecg.modules.tplus.service.ITplusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 供应商信息表
 * @Author: jeecg-boot
 * @Date:   2025-08-22
 * @Version: V1.0
 */
@Service
public class WhSupplierServiceImpl extends ServiceImpl<WhSupplierMapper, WhSupplier> implements IWhSupplierService {
    @Autowired
    private ITplusService tplusService;
    @Override
    public void handleSupplier() {
        List<WhSupplier> whLocationList = this.list();
        if(CollectionUtil.isNotEmpty(whLocationList)){
            baseMapper.deleteBatchIds(whLocationList);
        }
        String partnerCode = "NP01,NP02,NP03,NP04,NP05,WP01";
        for (String code : partnerCode.split(",")){
            PartnerQueryReqDto partnerQueryReqDto = new PartnerQueryReqDto();
            PartnerQueryReqDto.PartnerQueryParam param = new PartnerQueryReqDto.PartnerQueryParam();
            PartnerQueryReqDto.PartnerClass partnerClass = new PartnerQueryReqDto.PartnerClass();
            partnerClass.setCode(code);
            param.setPartnerClass(partnerClass);
            partnerQueryReqDto.setParam(param);
            List<PartnerQueryRepDto> partnerQueryRepDtos = tplusService.doPartnerQuery(partnerQueryReqDto);
            List<WhSupplier> locations = new ArrayList<>();
            for (PartnerQueryRepDto partnerQueryRepDto : partnerQueryRepDtos) {
                WhSupplier supplier = new WhSupplier();
                supplier.setCode(partnerQueryRepDto.getCode());
                supplier.setName(partnerQueryRepDto.getName());
                locations.add(supplier);
            }
            super.saveOrUpdateBatch(locations);
        }

    }
}
